home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 April / macformat-049.iso / mac / Shareware Plus / Sound / DSM Player 1.0 / DSM API 1.04 / dsm.h next >
Encoding:
C/C++ Source or Header  |  1997-01-11  |  12.9 KB  |  416 lines  |  [TEXT/CWIE]

  1. /*
  2. **  File:             dsm.h
  3. **  Description:    main header file
  4. **
  5. **    Digital Sound Machine v1.0, 
  6. **      © 1996 Dmitry Boldyrev
  7. **
  8. */
  9.  
  10. #pragma once
  11.  
  12. #ifndef __SOUND__
  13. #include <Sound.h>
  14. #endif
  15. #include "iostream.h"
  16.  
  17. #define __USE_ASM__     1
  18.  
  19. // =========================================================================================
  20. //    Sample Mode (sample mode field)
  21. // =========================================================================================
  22.  
  23. enum {
  24.     SM_REVERSE             = 2,                            //    Play sample backwards flag
  25.     SM_16BIT            = 4,                            //    16-bit sample flag (not supported)
  26.     SM_LOOP                = 8,                            //    Looping flag
  27.     SM_BIDI                = 16                            //    Ping-pong looping flag
  28. };
  29.  
  30. // =========================================================================================
  31. //    Playback Mode (play mode field)
  32. // =========================================================================================
  33.  
  34. enum {
  35.     PM_STEREO            = 1,                            //    Stereo mixing flag
  36.     PM_INTERP            = 2,                            //    Lerping flag
  37.     PM_SURROUND            = 4,                            //    Surround stereo flag
  38.     PM_16BIT            = 8                                //    16-bit mixing flag
  39. };
  40.  
  41. // =========================================================================================
  42. //    Status Attributes
  43. // =========================================================================================
  44.  
  45. enum {
  46.     ST_ClockChanged     = 1,
  47.     ST_BPMChanged        = 2,
  48.     ST_PatChanged        = 4
  49. };
  50.  
  51. #define    MAXQLEN            5
  52.  
  53. class Module;
  54.  
  55. // =========================================================================================
  56. //    Mixing Freqencies
  57. // =========================================================================================
  58.  
  59. typedef enum 
  60. {
  61.     mix44kHz    = 44100,                                //    44 kHz mixing
  62.     mix33kHz    = 33075,                                //    33 kHz mixing
  63.     mix22kHz     = 22050,                                //    22 kHz mixing
  64.     mix11kHz     = 11025                                    //    11 kHz mixing                    
  65. } MixRate;
  66.  
  67. class Mixer
  68. {
  69.     public:
  70.     
  71.                             Mixer();
  72.         virtual               ~Mixer();
  73.  
  74.         // =====================================================================================
  75.         //    Channel control methods
  76.         // =====================================================================================
  77.         
  78.         void                 SetVolume  (ubyte voice, ubyte volume);
  79.         void                SetFreq       (ubyte voice, ulong freq);
  80.         void                SetBalance (ubyte voice, ubyte pan);
  81.         void                VoicePlay  (ubyte voice, ulong start, ubyte sample);
  82.         
  83.         // =====================================================================================
  84.         //    Overridable methods
  85.         // =====================================================================================
  86.  
  87.         virtual void        PlayStart();
  88.         virtual void        PlayStop();
  89.  
  90.         virtual void        HandleTick(void);
  91.     
  92.         // =====================================================================================
  93.         //    Access methods
  94.         // =====================================================================================
  95.  
  96.         void                SetBPM(ulong inBPM);
  97.         ulong                GetBPM(void)           const;
  98.         
  99.         void                SetMixFreq(MixRate inMixFreq);
  100.         MixRate             GetMixFreq(void)       const;
  101.  
  102.         void                SetMixChannels(ubyte inNumChannels);
  103.         ubyte                GetMixChannels(void)  const;
  104.  
  105.         void                SetMixMode(ubyte inMode);
  106.         ubyte                GetMixMode()           const;        
  107.  
  108.         bool                IsActive()               const;
  109.  
  110.         void                SetGlobalVol(ubyte inVol);
  111.         ubyte                GetGlobalVol(void)    const;    
  112.  
  113.         byte*                GetMixingBuffer(void) const;
  114.         uword                 GetNumSamples(void)   const;
  115.         uword                GetNumBytes(void)       const;
  116.         
  117.         
  118.         void                 SetInstTab(struct SAMPLE **inInstTab);
  119.         struct SAMPLE **    GetInstTab(void)       const;
  120.  
  121.         void                UpdateMixer(void);
  122.         
  123.     protected:
  124.  
  125.         inline void         DTInstall(DeferredTaskPtr dtTaskPtr);
  126.         inline void            DTInit();
  127.         inline void            DTQuit();
  128.         
  129.         inline void            StartMixing();
  130.         inline void            StopMixing();
  131.  
  132.         inline uword         SamplesToBytes(uword samples) const;
  133.         inline uword         BytesToSamples(uword bytes) const;
  134.  
  135.         inline long         MixStereoNormal    (byte *srce, long *dest, ulong index, ulong increment, long todo);
  136.         inline long         MixStereoInterp    (byte *srce, long *dest, ulong index, ulong increment, long todo);
  137.         inline long         MixMonoNormal      (byte *srce, long *dest, ulong index, ulong increment, long todo);
  138.         inline long            MixMonoInterp      (byte *srce, long *dest, ulong index, ulong increment, long todo);
  139.  
  140.         inline void         Mix32To16Surround  (
  141.                                 register word *dste, 
  142.                                 register long *srce,
  143.                                 register long count);
  144.         inline void         Mix32To16Normal    (
  145.                                 register word *dste, 
  146.                                 register long *srce,
  147.                                 register long count);
  148.         inline void         Mix32To8Surround   (
  149.                                 register byte *dste, 
  150.                                 register long *srce,
  151.                                 register long count);
  152.         inline void         Mix32To8Normal     (
  153.                                 register byte *dste, 
  154.                                 register long *srce,
  155.                                 register long count);
  156.     
  157.     
  158.         inline void            AddChannel  (long *ptr, uword todo);
  159.     
  160.         void                ClearMixer(void);
  161.     
  162.         static pascal void     MyDeferredDoubleBackProc(SndChannelPtr chan, SndDoubleBufferPtr doubleBuffer);
  163.  
  164. #if GENERATINGCFM
  165.         static pascal void     MyDeferredTask(struct MixerQElem* dtParam);
  166. #else
  167.         static pascal void     MyDeferredTask(struct MixerQElem* dtParam:__A1);
  168. #endif
  169.             
  170.         inline void         WriteSamples(byte *databuf, uword size);
  171.         inline void            AdjustTicksLeft();
  172.     
  173.         // =====================================================================================
  174.         //    General data
  175.         // =====================================================================================
  176.     
  177.          long*                mLVolSel;                    //    Left channel volume select
  178.          long*                mRVolSel;                    //    Right channel volume select
  179.  
  180.          long*                mStereoDelayBuf;             //     Stereo delay buffer
  181.         uword                 mSDReadPos;                    //    Stereo delay READ position
  182.         uword                 mSDWritePos;                //    Stereo delay WRITE position
  183.         uword                mStereoDelay;                //    Stereo delay adj
  184.         
  185.          long*                mReverbBuf;                 //     Reverb buffer
  186.         uword                 mRVReadPos;                    //    Reverb READ position
  187.         uword                 mRVWritePos;                //    Reverb WRITE position
  188.         uword                mReverb;                    //    Reverb adj
  189.  
  190.         struct VINFO*        mChanTab;                    //    all channels
  191.         struct VINFO*        mChanRampTab;                //    all channels
  192.         struct VINFO*         mCurChan;                    //    current channel
  193.         ulong                 mSamples;                    //    number of samples to mix
  194.          long                 mIdxSize;                    //    length of sample
  195.          long                mIdxLPos;                    //    position of sample
  196.          long                mIdxLEnd;                    //    loop end of sample
  197.          
  198.          long*                 TICKBUF;                    //    mixing buffer
  199.          long                TICKLEFT;                    //    number of ticks left
  200.          
  201.          byte                mMode;                        //    playing mode
  202.         MixRate                mMixFreq;                    //    natural frequency
  203.         ulong                mBPM;                        //    beats per minute
  204.         ubyte                mMixChannels;                //    num of channels being mixed
  205.         ubyte                mGlobalVol;                    //    Software Global volume
  206.          bool                mActive;                    //    true, if mixer is mixing
  207.          long                mTicksLeft;                    //    MixFreq / BPM ratio.
  208.         struct SAMPLE**        mInstTab;                    //    instrument table
  209.          long                 mVolumeTab[65][256];        //    volume table
  210.          bool                mStopPending;                //    Awaiting flag for synth
  211.          long                mNumFrames;                    //    Number of frames
  212.         ubyte                mAmpShift;
  213.          byte*                mMixingData;                //    Pointer to current mixing buffer
  214.                  
  215.         // =====================================================================================
  216.         //    Macintosh specific info
  217.         // =====================================================================================
  218.         
  219.         static DeferredTaskUPP        gDefTaskProc;        //    Mac deferred task desc
  220.         static SndDoubleBackUPP        gDoubleBackProc;    //     Mac double back task desc
  221.         
  222.         SndChannelPtr        mSndChannel;                //    Mac channel sturct
  223.         SndDoubleBufferHeader mDoubleHeader;            //    Mac header struct
  224.         long                mGlobA5;                    //    Mac global A5 pointer
  225.         QHdr                mQueue;                        //    Mac sound buffer queue    
  226.         struct MixerQElem*    mEntryArr[MAXQLEN];            //    Mac sound buffer queue entry
  227.          byte                mQEntries;                    //    Number of entries in queue
  228. };
  229.  
  230. class Module;
  231.  
  232. class Synthesiser :
  233.     public Mixer
  234. {
  235.     public:
  236.                             Synthesiser(
  237.                                 ubyte     mode, 
  238.                                 MixRate mixFreq  = mix22kHz, 
  239.                                 bool    looping = false);
  240.                                            
  241.         virtual                  ~Synthesiser();
  242.                    
  243.         void                AttachMod(Module& mod);
  244.         
  245.         virtual void        PlayStart();
  246.         virtual void        PlayStop(Boolean inSlideVol = false);
  247.         
  248.         static inline long     MACfreq(uword period);
  249.         static inline word     Fine2Hz(ubyte ft);
  250.             
  251.         virtual void        GoToNextPattern(void);
  252.         virtual void        GoToPrevPattern(void);
  253.     
  254.         ubyte                TogglePause(void);
  255.     
  256.         bool                 IsPlaying(void)      const;
  257.         bool                IsPaused(void)          const;
  258.  
  259.         ubyte                GetNumChannels(void) const;
  260.         ubyte                 GetNumPatterns()      const;
  261.         ubyte                 GetCurPattern()      const;
  262.     
  263.         ulong                GetCurClock()          const;
  264.         bool                 IsDone()              const;
  265.  
  266.         char*                GetModName()          const;
  267.         
  268.         ubyte                GetStatus()          const;
  269.         void                SetStatus(ubyte inStat);
  270.         
  271.     protected:
  272.                             
  273.         inline void            doTremor     (ubyte track);
  274.         inline void         doPorta      (ubyte track);
  275.         inline void         doVibrato    (ubyte track);
  276.         inline void         doFineVibrato(ubyte track);
  277.         inline void         doVolslide   (ubyte track);
  278.         inline void         doTremolo    (ubyte track);
  279.  
  280.         inline void            UpdateEffect (uword tick);
  281.         void         UpdateNote   (void);
  282.     
  283.         inline void            ClearChannels(void);
  284.         
  285.         virtual void        HandleTick  (void);
  286.         
  287.         ubyte                mMasterVol;                //    Master volume    
  288.  
  289.         
  290.         struct CHANNEL*        mChannel;                //    channels info
  291.         struct NOTE*        mCurrent;                //    current note pointer
  292.         
  293.         struct NOTE**        mPatBuf;                //    pattern table
  294.         ubyte*                mOrder;                    //    order table
  295.         ubyte*                mPatLen;                //    length of pattern table
  296.         ubyte                mNumChans;                //    number of channels used by song
  297.         uword                mNumOrds;                //    song length
  298.         ubyte                mSpeed;                    //    song speed
  299.         
  300.         word                mOrd;                    //    current order
  301.         ubyte                mRow;                    //    current row
  302.         ubyte                mPatDelay;                //    pattern delay flag
  303.         ubyte                mFlags;                    //    other song flags
  304.         ubyte                mLimVol;                //    limiting volume
  305.         bool                mPause;                    //    pause flag (on/off)
  306.         bool                mSongLoop;                //    song looping (on/off)
  307.         bool                mDone;
  308.         ulong                mClock;                    //    current clock
  309.         
  310.         Module*                mMod;                    //    module link
  311.  
  312.         ubyte                mStatus;                //     engine status attribute
  313. };
  314.  
  315. // =========================================================================================
  316. //    Module (must be overriden. basically, provides the driver io interface)
  317. // =========================================================================================
  318.  
  319. class Module
  320. {
  321.     public:
  322.     
  323.                             Module();
  324.         virtual               ~Module();
  325.         
  326.         // =====================================================================================
  327.         //    Load song
  328.         // =====================================================================================
  329.     
  330.         virtual error        LoadSong(void);
  331.  
  332.         // =====================================================================================
  333.         //    Acess methods
  334.         // =====================================================================================
  335.         
  336.         error                getError(void)  const;    //    Returns error code
  337.         
  338.         ubyte                GetLimVol()     const;    //    Returns limiting volume
  339.         
  340.         struct SAMPLE**        GetInstTab()    const;    //     Returns Instrument Table
  341.         struct NOTE**        GetPatTab()     const;    //    Returns Pattern Table
  342.         ubyte*                GetOrderTab()   const;    //    Returns Order Table
  343.         ubyte*                GetPatLenTab()  const;    //    Returns Pattern Length Table
  344.         ubyte*                GetPanTab()     const;    //    Returns Panning Table
  345.         
  346.         ubyte                GetNumChans()   const;    //    Returns Number of Channels
  347.         uword                GetNumOrds()    const;    //    Returns Number of Orders
  348.         ubyte                GetSpeed()      const;    //    Returns Song Speed
  349.         ubyte                GetRestartPos() const;    //    Returns Restart Position
  350.         ubyte                GetFlags()      const;    //    Returns Song Flags
  351.         ubyte                GetBPM()        const;    //    Returns Beats Per Minute ratio
  352.         ubyte                GetMasterVol()    const;    //    Returns Master Volume
  353.         
  354.         char*                GetName()       const;    //    Returns Song Name
  355.     
  356.     protected:
  357.  
  358.         inline error         LoadInstrument(ubyte instno, ubyte type);
  359.  
  360.         inline error        ImportS3M(void);        //    Imports Scream Tracker Mod format
  361.         inline error        ImportMOD(void);        //    Imports Pro Tracker Mod format
  362.         inline error         ImportMTM(void);        //    Imports MultiTracker Mod format
  363.         inline error         Import669(void);        //    Imports Studio 669 Mod format
  364.         inline error        ImportFAR(void);        //    Imports Farandole Tracker Mod format
  365.     
  366.         ios*                fp;
  367.         
  368.         SAMPLE*                mInstTab[256];            //    instrument table
  369.         NOTE*                mPatBuf[256];            //    pattern buffer
  370.         
  371.         ubyte                mPanTab[32];            //    default pan table
  372.         ubyte                mOrder[256];            //    order table
  373.         ubyte                mPatLen[256];            //    pattern length table
  374.         
  375.         ubyte                mMasterVol;                //    Master volume    
  376.         char                mName[40];                //    name of the module
  377.         
  378.         ubyte                mNumChans;                //    number of channels
  379.         uword                 mNumInsts;                //    number of instruments
  380.         uword                mNumPats;                //    number of patterns (unused)
  381.         uword                mNumOrds;                //    length of order table
  382.         
  383.         ubyte                mRestart;                //    restart position
  384.         ubyte                mFlags;                    //    song flags
  385.         ubyte                mSpeed;                    //    song speed
  386.         ubyte                mBPM;                    //    "beats per minute" ratio
  387.         ubyte                mLimVol;                //    limiting volume
  388.         
  389.         char*                mMessage;                //    module message
  390.         
  391.         word                mError;                    //    stores the error
  392. };
  393.  
  394. class FileMod :
  395.     public Module
  396. {
  397.     public:
  398.                             FileMod(const Str255 fName, short vRefNum);
  399.                             FileMod(const SFReply &inReply);
  400.                             FileMod(const FSSpec &inSpec);
  401. };
  402.  
  403. class ResMod : 
  404.     public Module
  405. {
  406.     public:
  407.                             ResMod(ResType rType, short rID);                            
  408. };
  409.  
  410. class MemMod : 
  411.     public Module
  412. {
  413.     public:
  414.                             MemMod(Ptr base, long length);                            
  415. };
  416.